<?php

/**
 * 实现动作参数权限访问
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.shumenol.com)
 * @version $Id: Param.php 735 2011-08-09 02:23:19Z mmxie $
 */
class IG_Acl_Assert_Param implements Zend_Acl_Assert_Interface
{
    protected $_params;

    protected $_resourceId;

    protected $_roleId;

    public function __construct($resourceId, $roleId, $params)
    {
        $this->_resourceId = (int) $resourceId;
        $this->_roleId     = (int) $roleId;
        $this->_params     = (array) $params;
    }

    public function assert(Zend_Acl $acl,
                           Zend_Acl_Role_Interface $role = null,
                           Zend_Acl_Resource_Interface $resource = null,
                           $privilege = null)
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $resourceParam = new System_Model_ResourceParam;
        $allow = 0;
        foreach ($this->_params as $param) {
            if ($request->getParam($param) === null) {
                return false;
            }

            $resourceParam->count(array(
                'resource_id=?' => $this->_resourceId,
                'role_id=?'     => $this->_roleId,
                'param_key=?'   => $param,
                'param_value=?' => (int) $request->getParam($param),
            )) && $allow++;
        }

        return $allow == count($this->_params);
    }
}